import axiosFactory from './axiosFactory'
import { procApiCb, procApiError } from '../util'

export default {
  getApi(getPath, parms) {
    return new Promise((resolve, reject) => {
      axiosFactory.create().get(getPath, {
        params: parms
      }).then(res => {
        procApiCb(res, resData => {
          resolve(resData)
        }, errMsg => {
          reject(errMsg)
        })
      }).catch(err => {
        procApiError(err, errMsg => {
          reject(errMsg)
        })
      })
    });
  },
  getUrlApi(getPath, urlPath) {
    return new Promise((resolve, reject) => {
      axiosFactory.create().get(getPath + '/' + urlPath).then(res => {
        procApiCb(res, resData => {
          resolve(resData)
        }, errMsg => {
          reject(errMsg)
        })
      }).catch(err => {
        procApiError(err, errMsg => {
          reject(errMsg)
        })
      })
    });
  },
  postApi(postPath, jsonParms) {
    return new Promise((resolve, reject) => {
      axiosFactory.create().post(postPath, jsonParms).then(res => {
        procApiCb(res, resData => {
          resolve(resData)
        }, errMsg => {
          reject(errMsg)
        })
      }).catch(err => {
        procApiError(err, errMsg => {
          reject(errMsg)
        })
      })
    });
  }
}